package com.meirengu.webview.service.impl;

import com.meirengu.channel.service.HeiNiuBaoService;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.webview.dao.KwChannelDataDao;
import com.meirengu.webview.dao.UserDao;
import com.meirengu.webview.model.KwChannel;
import com.meirengu.webview.model.User;
import com.meirengu.webview.service.KwChannelDataService;
import com.meirengu.webview.service.KwChannelService;
import com.meirengu.webview.service.UserInfoService;
import com.meirengu.webview.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * User服务实现层
 *
 * @author maoruxin
 * @create Sun Dec 10 09:58:45 CST 2017
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserDao userDao;

    @Autowired
    KwChannelDataDao kwChannelDataDao;

    @Autowired
    KwChannelService kwChannelService;

    @Autowired
    KwChannelDataService kwChannelDataService;

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    HeiNiuBaoService heiNiuBaoService;

    public Integer isExist(String mobile) {
        return userDao.isExist(mobile);
    }

    public User getByMobile(String mobile) {
        return userDao.getByMobile(mobile);
    }

    /**
     * 注册
     *
     * @param phoneno
     * @param name
     * @param sex
     * @param cid
     * @return
     */
    public int signUp(String phoneno, String name, Integer sex, String city, String birthday, String cid, String ip) throws Exception {

        //查询渠道
        KwChannel kwChannel = kwChannelService.getByCode(cid);
        User user = new User();
        user.setMobile(phoneno);
        user.setName(name);
//            user.setPassword();
        user.setSex(sex);
        user.setCity(city);
        user.setBirthday(DateUtils.getDate(birthday, null));
        int age = getAge(user.getBirthday());
        user.setAge(age);
        user.setIp(ip);
        user.setChannelCode(cid);
        if (kwChannel != null && StringUtils.isNotBlank(kwChannel.getId())) {
            user.setChannelId(kwChannel.getId());
            user.setChannelName(kwChannel.getChannelName());
        }
        int code = insert(user);
        if (StringUtils.isNotBlank(user.getId())) {
            //发送触发短信
            userInfoService.sendMQlinkwechat(user.getId());
            userInfoService.sendMQlinkMarketing(user.getId());
        }
        Map<String, String> map = new HashMap<>();
        map.put("channelId", cid);
        map.put("registNum", "1");
        map.put("visitNum", "1");
        kwChannelDataDao.addNum(map);
        //发送黑牛渠道
//        int aSex = (sex==0) ? 1:2;
//        logger.info("name: {}  heiSex:  {} phoneno:  {}  birthday:  {}",name,aSex,phoneno,birthday);
//        heiNiuBaoService.enhanced(name,aSex+"",phoneno,birthday);
        return code;
    }

    @Override
    public Integer getCountByTime(Map map) throws Exception {
        return userDao.getCountByTime(map);
    }

    @Override
    public List<Map<String, Object>> getAllMobile(Map map) {
        return userDao.getAllMobile(map);
    }

    //由出生日期获得年龄
    public int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;
    }

}
